package com.codename1.samples;


import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.location.Location;
import com.codename1.location.LocationManager;
import com.codename1.ui.Button;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class LocationSample {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World", BoxLayout.y());
        Button checkIfDetectionAvailable = new Button("Check if GPS Detection Available");
        checkIfDetectionAvailable.addActionListener(e->{
            String message = LocationManager.getLocationManager().isGPSDetectionSupported() ? "GPS Detection IS supported" : "GPS Detection is NOT supported";
            Dialog.show("GPS Detection Result", message, "OK", null);
        });
        
        Button isGPSEnabled = new Button("GPS Status");
        isGPSEnabled.addActionListener(e->{
            String message = LocationManager.getLocationManager().isGPSEnabled() ? "GPS IS enabled" : "GPS is NOT enabled";
            Dialog.show("GPS Enabled?", message, "OK", null);
            
        });
        
        Button getLocation = new Button("Get Location");
        getLocation.addActionListener(e->{
            Location loc = LocationManager.getLocationManager().getCurrentLocationSync(3000);
            String message = loc != null ? "Location is "+loc : "Location not found";
            Dialog.show("Location", message, "OK", null);
        });  
        hi.addAll(checkIfDetectionAvailable, isGPSEnabled, getLocation);
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
